<?php
/*
 +----------------------------------------------------------------------
 + Title        : MiniAppController 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2022/2/20 19:49
 + Last-time    : 2022/2/20 19:49+ chen2
 + Desc         : MiniAppController
 +----------------------------------------------------------------------
*/


namespace app\api\controller;

use app\BaseController;
use app\common\attributes\Menu;
use app\common\attributes\RandyValidate;
use app\common\model\Fans;
use app\common\service\FansService;
use app\common\service\MiniService;
use app\common\service\UserService;
use app\common\service\WechatService;
use app\user\validate\FansValidate;
use think\annotation\Inject;
use think\response\Json;

class MiniAppController extends BaseController
{

    #[Inject]
    protected FansService $service;

    public function login($code)
    {
        $userInfo = (new MiniService())->session($code);
//        $userInfo = [
//            "session_key" => "cJpb8gtJX5MZEUASb0dcyg==",
//            "openid"      => "oi5zA5aZ2uA_QH2VhMazVgLWlcxc",
//            "unionid"     => "oYTaL52VQ2G3vMwztYtm-Iw2BkGY"
//        ];
        if ($userInfo) {
            $user = UserService::loginByOpenid($userInfo['openid']);
            $user['session_key'] = $userInfo['session_key'];
            return $this->returnSuccess($user);
        }
        return $this->returnError('失败!');
    }

    /**
     * 授权用户信息
     * @author : Randy_chen
     * @Date: 2023/5/14
     * @Time: 7:59
     */
    #[RandyValidate(FansValidate::class ,'authUserInfo')]
    public function authUserInfo():Json{
        $data= $this->getParams();
        $user = $this->service->authUserInfo($data['fans_id'] ,$data['userInfo']);
        return $this->returnSuccess($user);
    }

    #[Menu('获取小程序用户手机号')]
    public function getMobileByCode($code ) : Json
    {
        $userInfo = (new MiniService())->getMobileByCode($code);
        return $this->returnSuccess($userInfo);
    }

    public function loginById($user_id)
    {
        $user = UserService::loginById($user_id);
        return $this->returnSuccess($user);
    }

}